package com.beehoo.dcportal.admin.action;

import java.util.List;

import com.beehoo.dcportal.admin.service.CommentService;
import com.beehoo.dcportal.common.Constant;
import com.beehoo.dcportal.common.base.BaseAction;
import com.beehoo.dcportal.common.base.BaseException;
import com.beehoo.dcportal.model.CommentBean;
import com.beehoo.dcportal.utils.CommonUtils;

public class CommentAction extends BaseAction {

	private static final long serialVersionUID = 5520248045278198069L;
	private CommentService commentService;
	private CommentBean commentBean;
	private List<CommentBean> resultList;
	private String commentState;
	private String beginDate;
	private String endDate;
	private String commentId;
	private int totalRows;                   //列表条目总数
	private int pageRows;                    //每页条目数
	private int pageNo;                      //当前页码
	private String naction;                   //请求地址

	public int getTotalRows() {
		return totalRows;
	}

	public void setTotalRows(int totalRows) {
		this.totalRows = totalRows;
	}

	public int getPageRows() {
		return pageRows;
	}

	public void setPageRows(int pageRows) {
		this.pageRows = pageRows;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public String getNaction() {
		return naction;
	}

	public void setNaction(String naction) {
		this.naction = naction;
	}

	public void setCommentService(CommentService commentService) {
		this.commentService = commentService;
	}

	public CommentBean getCommentBean() {
		return commentBean;
	}

	public void setCommentBean(CommentBean commentBean) {
		this.commentBean = commentBean;
	}

	public List<CommentBean> getResultList() {
		return resultList;
	}

	public void setResultList(List<CommentBean> resultList) {
		this.resultList = resultList;
	}

	public String getCommentState() {
		return commentState;
	}

	public void setCommentState(String commentState) {
		this.commentState = commentState;
	}

	public String getBeginDate() {
		return beginDate;
	}

	public void setBeginDate(String beginDate) {
		this.beginDate = beginDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public String getCommentId() {
		return commentId;
	}

	public void setCommentId(String commentId) {
		this.commentId = commentId;
	}

	public String list() {
		initDate();
		String streetId = (String) getSession().get(Constant.SESSION_STREET_ID);
		if(CommonUtils.nvlToString(commentState).equals(""))
			commentState = "0";
		
		pageRows = Constant.ADMIN_DEFAULT_ROWS;
		if(pageNo == 0)
			pageNo = 1;
		
		try {
			resultList = commentService.queryComments(streetId, commentState,
					beginDate, endDate, pageNo, pageRows);
			totalRows = commentService.queryCommentCount(streetId,
					commentState, beginDate, endDate);
			naction = "/admin/comment!list.action";
		} catch (BaseException e) {
			setMessage(super.getText(e.getMessage()));
		}
		return SUCCESS;
	}

	public String initAudit() {
		String streetId = (String) getSession().get(Constant.SESSION_STREET_ID);
		try {
			commentBean = commentService.queryComment(streetId, commentId);
		} catch (BaseException e) {
			setMessage(super.getText(e.getMessage()));
		}
		return "audit";
	}

	public String audit() {
		try {
			String auditorId = (String) getSession().get(Constant.SESSION_USER_ID);
			String auditor = (String) getSession().get(Constant.SESSION_USER_NAME);
			if(commentBean != null){
				commentBean.setAuditorId(auditorId);
				commentBean.setAuditor(auditor);
			}
			commentService.auditComment(commentBean);
		} catch (BaseException e) {
			setMessage(super.getText(e.getMessage()));
		}
		return list();
	}

	public String del() {
		try {
			commentService.delComment(commentId);
		} catch (BaseException e) {
			setMessage(super.getText(e.getMessage()));
		}
		return list();
	}
	
	private void initDate() {
		if(CommonUtils.nvlToString(beginDate).equals(""))
			beginDate = CommonUtils.getPastDay(7);
		if(CommonUtils.nvlToString(endDate).equals(""))
			endDate = CommonUtils.getDate();
	}
}
